package com.teangtang.fly.controller;

import com.teangtang.fly.dto.CabinDTO;
import com.teangtang.fly.dto.PlaneDTO;
import com.teangtang.fly.entity.Cabin;
import com.teangtang.fly.entity.Plane;
import com.teangtang.fly.entity.Province;
import com.teangtang.fly.service.CabinService;
import com.teangtang.fly.service.PlaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/")
public class PlaneAndCabinController {

    @Autowired
    private PlaneService planeService;

    @Autowired
    private CabinService cabinService;

    @PutMapping("/plane/{id}")
    public PlaneDTO updatePlane(@PathVariable("id") Long id, @RequestBody PlaneDTO planeDTO) {
        // 根据 id 查询 Plane 实体
        Plane plane = planeService.getPlaneById(id);
        // 更新 Plane 实体
        return planeService.savePlaneByPlaneDTO(plane,planeDTO);
    }

    @PostMapping("/plane")
    public PlaneDTO createPlane(@RequestBody PlaneDTO planeDTO) {
        // 将 PlaneDTO 转换为 Plane 实体保存到数据库中
        Plane plane = new Plane();
        return planeService.savePlaneByPlaneDTO(plane,planeDTO);
    }

    @GetMapping("/planes")
    public ResponseEntity<List<Plane>> getAllProvinces() {
        List<Plane> planes = planeService.getAllPlanes();
        return new ResponseEntity<>(planes, HttpStatus.CREATED);
    }
    @DeleteMapping("/plane/{id}")
    public ResponseEntity<Void> deletePlane(@PathVariable("id") Long id) {
        planeService.deletePlaneById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
